﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utility;
using Entity;

namespace StudentAppointmentPlatform
{
    public partial class ShowMessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userId = Session["UserId"] as string;
            if (string.IsNullOrWhiteSpace(userId))
            {
                Alert.AlertOnPage(Page, "您可能未登录，或系统出错，即将跳转到默认页面。");
                Redirect.Js_Redirect(Page, "/Default.aspx");
            }

            int uid = UserBO.GetIdByUserId(userId);
            if (uid != -1)
            {
                List<Message> unreadMessages = MessageBO.GetUnreadMessages(uid);
                List<Message> readMessages = MessageBO.GetReadMessages(uid);

                AddControlsToView(unreadMessages, ref vUnreadMessage);
                AddControlsToView(readMessages, ref vReadMessage);              
            }

            if (!IsPostBack)
            {
                blMessageType.Items[0].Selected = true;
                blMessageType.SelectedItem.Attributes.Add("class", "selected");
                //blMessageType.Items[0].Attributes.Add("style", "font-weight:bold; background-color:#799fc3; color:#465c71;");
            }
        }

        /// <summary>
        /// 装配用户控件并添加到View中;
        /// </summary>       
        protected void AddControlsToView(List<Message> messages, ref View view)
        {
            foreach (Message message in messages)
            {
                MessageBlock messageBlock = (MessageBlock)LoadControl("MessageBlock.ascx");
                messageBlock.Message = message;
                messageBlock.SetInfo();

                view.Controls.Add(messageBlock);
            }
        }

        protected void blMessageType_OnClick(object sender, BulletedListEventArgs e)
        {
            mvMessage.ActiveViewIndex = e.Index;
            //blMessageType.Items[e.Index].Attributes.CssStyle["font-weight"] = "bold";
            //blMessageType.Items[e.Index].Attributes.Add("style", "font-weight:bold; background-color:#799fc3; color:#465c71;");
            blMessageType.Items[e.Index].Attributes.Add("class", "selected");
        }
    }
}